package com.actor.networking;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.FastInput;
import com.esotericsoftware.kryo.io.FastOutput;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.io.ByteArrayOutputStream;

/**
 * @author Dmitry
 */
public class KryoSerializer extends Serializer {
    private final Kryo kryo;

    public KryoSerializer() {
        kryo = new Kryo();
        kryo.register(NetworkMessage.class);
        kryo.register(NetworkMessageType.class);
    }

    @Override
    public byte[] serialize(NetworkMessage value) {
        Output output = new FastOutput(new ByteArrayOutputStream(4096));
        return output.toBytes();
    }

    @Override
    public NetworkMessage deserialize(byte[] array) {
        Input input = new FastInput(array);
        return kryo.readObject(input, NetworkMessage.class);
    }

}
